package com.example.sensor.slice;

import com.example.sensor.GeoConvertAbility;
import com.example.sensor.ResourceTable;
import com.example.sensor.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.location.GeoAddress;
import ohos.location.GeoConvert;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GeoConvertAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button mBtnGeocoding;
    private Button mBtnReverseGeocoding;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_geo_convert);

        mBtnGeocoding = (Button) findComponentById(ResourceTable.Id_btn_geocoding);
        mBtnGeocoding.setClickedListener(this);
        mBtnReverseGeocoding = (Button) findComponentById(ResourceTable.Id_btn_reverse_geocoding);
        mBtnReverseGeocoding.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {

        // 地理编码
        if (component.getId() == ResourceTable.Id_btn_geocoding) {
            // 创建GeoConvert对象
            GeoConvert geoConvert = new GeoConvert(Locale.getDefault());
            // 访问互联网，异步处理
            getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() ->{
                try {
                    // 地理编码
                    final List<GeoAddress> geoAddresses =
                            geoConvert.getAddressFromLocationName(
                                    "长春市双阳区雕塑公园",
                                    1);
                    // 打印经纬度
                    if (geoAddresses != null && geoAddresses.size() > 0) {
                        getUITaskDispatcher().asyncDispatch(()-> {
                            // 获取经度
                            double longitude = geoAddresses.get(0).getLongitude();
                            // 获取纬度
                            double latitude = geoAddresses.get(0).getLatitude();
                            Utils.showToast(GeoConvertAbilitySlice.this,"经度: " + longitude
                                    + " 纬度: " + latitude);
                        });
                    }
                } catch (Exception e) {
                    Utils.log("异常: " + e.getLocalizedMessage());
                }
            });
        }

        // 逆地理编码
        if (component.getId() == ResourceTable.Id_btn_reverse_geocoding) {
            // 创建GeoConvert对象
            GeoConvert geoConvert = new GeoConvert(Locale.getDefault());
            // 访问互联网，异步处理
            getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() ->{
                try {
                    // 逆地理编码
                    List<GeoAddress> geoAddresses = geoConvert.getAddressFromLocation(
                            39.861861,
                            119.253937,
                            1);
                    // 打印地址
                    if (geoAddresses != null && geoAddresses.size() > 0) {
                        getUITaskDispatcher().asyncDispatch(()-> {
                            Utils.showToast(GeoConvertAbilitySlice.this,
                                    "地址:" + geoAddresses.get(0).getPlaceName());
                        });
                    }
                } catch (Exception e) {
                    Utils.log("异常: " + e.getLocalizedMessage());
                }
            });
        }


    }
}
